package com.fxhy.utils;

import java.net.URL;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class LocationUtil {
//	public static void main(String[] args) {
//        String add = getAdd(113.8981683, 22.5634233); 
//    }  
    
	/**
	 * 通过经纬度获取地址
	 * @param jing 经度
	 * @param wei 纬度
	 * @return
	 */
	public static String getAdd(double jing, double wei ){  
        //lat 小  log  大  
        //参数解释: 纬度,经度 type 001 (100代表道路，010代表POI，001代表门址，111可以同时显示前三项)  
        String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+wei+","+jing+"&type=010";
        String res = "";     
        try {     
            URL url = new URL(urlString);    
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();    
            conn.setDoOutput(true);    
            conn.setRequestMethod("POST");    
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));    
            String line;    
           while ((line = in.readLine()) != null) {    
               res += line+"\n";    
         }    
            in.close();    
        } catch (Exception e) {    
            System.out.println("error in wapaction,and e is " + e.getMessage());    
        }   
        System.out.println(res);  
        
        JSONObject jsonObject = JSONObject.fromObject(res);  
        JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));  
        JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0));  
        String allAdd = j_2.getString("admName");  
        String arr[] = allAdd.split(",");
        String address = arr[0]+arr[1]+arr[2]+j_2.getString("addr")+j_2.getString("name");
        String allAddStr = arr[0]+"-"+arr[1]+"-"+arr[2]+"-"+address; 
        System.out.println(allAddStr);
        
        return allAddStr;    
    } 
}
